package org.spiderflow.core.expression.parsing.ast.literal;

import org.spiderflow.core.expression.ExpressionTemplate;
import org.spiderflow.core.expression.ExpressionTemplateContext;
import org.spiderflow.core.expression.parsing.Span;
import org.spiderflow.core.expression.parsing.ast.Expression;

import java.io.IOException;

/**
 * A single precision floating point literla. May be optionally marked with the <code>f</code> suffix, e.g. "1.0f".
 **/
public class FloatLiteral extends Expression {
    private final Float value;

    public FloatLiteral(Span literal) {
        super(literal);
        String text = literal.getText();
        if (text.charAt(text.length() - 1) == 'f') {
            text = text.substring(0, text.length() - 1);
        }
        this.value = Float.parseFloat(text);
    }

    public Float getValue() {
        return value;
    }

    @Override
    public Object evaluate(ExpressionTemplate template, ExpressionTemplateContext context) throws IOException {
        return value;
    }
}
